package com.voltmao.service.application.sys;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.voltmao.persistent.mapper.SysRegionMapper;
import com.voltmao.persistent.model.SysRegionCriteria;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.utils.NumberUtils;
import com.voltmao.webpro.utils.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 渠道商合同管理服务接口
 *
 * @author zhanglin
 * @date 2018/04/13
 */
@Service
public class RegionServiceImpl {

    private static final Logger testLogger = LoggerFactory.getLogger("MY_TEST_LOGGER");
    @Autowired
    SysRegionMapper sysRegionMapper;

    /**
     * 根据code获取对应的region_name
     * codeValues :"code1,code2,..."
     * 返回"rn1,rn2,rn3..."
     * @param codeValues code
     * @return 对应name
     */
    public String codes2RegionNames(String codeValues){
        testLogger.info("带查询参数："+codeValues);
        String[] codeList = codeValues.split(",");
        StringBuilder tmpStr = new StringBuilder();
        List<Map<String, Object>> names = sysRegionMapper.getRegionNameByCode(codeList);
        for(int i=0;i<names.size();i++){
            String regionName = names.get(i).get("regionName")+"";
            tmpStr.append(regionName);
            if(i<codeList.length-1){
                tmpStr.append(",");
            }
        }
        return tmpStr.toString();
    }

    public DataPage selectRegionDataPage(Map<String, Object> params){
        testLogger.info("待查询参数："+params.toString());
        DataPage dataPage = new DataPage();
        int pageIndex = StringUtils.parseInt(params.get("pageNumber")+"", 1);
        int pageSize = StringUtils.parseInt(params.get("pageSize")+"", 20);

        SysRegionCriteria example = new SysRegionCriteria();
        SysRegionCriteria.Criteria c = example.createCriteria();
        c.andIsDelEqualTo(0);
        if(params.size() > 0){
            String regionName = "regionName";
            if(StringUtils.isNotEmpty(params.get(regionName))) {
                //具体项模糊查询
                c.andRegionNameLike("%" + params.get(regionName) + "%");
            }
            String code = "code";
            if(StringUtils.isNotEmpty(params.get(code))){
                //code查询
                c.andCodeEqualTo(params.get(code)+"");
            }
            String provinceName = "provinceName";
            if(StringUtils.isNotEmpty(params.get(provinceName))) {
                //省级模糊查询
                c.andProvinceNameLike("%" + params.get(provinceName) + "%");
            }
            String cityName = "cityName";
            if(StringUtils.isNotEmpty(params.get(cityName))) {
                //市级模糊查询
                c.andCityNameLike("%" + params.get(cityName) + "%");
            }
            String level = "level";
            if(StringUtils.isNotEmpty(params.get(level))) {
                //等级
                c.andLevelEqualTo(NumberUtils.parseInt(params.get(level)+"",1));
            }
        }
        example.setPageSize(pageSize);
        example.setLimitStart((pageIndex - 1) * pageSize);

        dataPage.pageindex= pageIndex;
        dataPage.pagesize = pageSize;
        dataPage.rows = sysRegionMapper.selectMapByExample(example);
        dataPage.total = sysRegionMapper.countByExample(example);
        return dataPage;
    }
}
